package com.qidian.md.service.impl;

import com.qidian.md.mapper.WorkshopMapper;
import com.qidian.md.service.WorkshopService;
import com.qidian.mes.pojo.md.Workshop;
import com.qidian.mes.util.pojo.MESResult;
import com.qidian.mes.util.pojo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.WatchService;

/**
 * @Author 18林梦雅
 * @Company 起点编程
 * @Time 2023/10/31 14:24
 * @Description
 */
@Service
public class WorkshopServiceImpl implements WorkshopService {

@Autowired
    WorkshopMapper workshopMapper;




    @Override
    public MESResult queryByPage(Workshop workshop, int page, int limit) {
        Pager<Workshop> pager=new Pager<>();
        pager.setCurr(page);
        pager.setLimit(limit);
        pager.setTotal( workshopMapper.selectCount(workshop));
        pager.setData(workshopMapper.selectByPage(workshop,(page-1)*limit,limit));
        pager.setMaxPageNum((int) Math.ceil(pager.getTotal()*1.0/limit));
        return MESResult.ok(pager);

    }


    /**
     * 添加车间,先判断车间名是否重复,再添加
     * @param workshop
     * @return
     */
    @Override
    public MESResult addWorkshop(Workshop workshop) {

        return MESResult.updateIsOk(workshopMapper.addWorkshop(workshop));
    }



    /**
     * 先根据Id查询内容,返回给前端的编辑页面
     * @param workshopId
     * @return
     */
    @Override
    public MESResult queryById(Long workshopId) {
        Workshop workshop = null;
        if ((workshop = workshopMapper.selectById(workshopId)) != null){
            return MESResult.ok(workshop);
        }
        return MESResult.empty();
    }



    /**
     * 根据车间id-->车间修改编辑
     * 直接更新数据,不用判断车间名是否重复,编码是不一样的
     * @param workshop
     * @return
     */

    @Override
    public MESResult update(Workshop workshop) {

        return MESResult.updateIsOk(workshopMapper.update(workshop));

    }




    /**
     * 单行删除功能
     * @param workshopId
     * @return
     */
    @Override
    public MESResult deleteUpdateStatus(Long workshopId) {
        return MESResult.ok(workshopMapper.deleteUpdateStatus(workshopId));
    }






    /**
     * 选中多行删除功能
     * @param workshopId
     * @return
     */
    @Override
    public MESResult deleteUpdateSelect(Long[] workshopId) {
        int count=workshopMapper.deleteUpdateSelect(workshopId);
        return MESResult.ok(count+" ");
    }
}
